package main

import (
	"context"
	"fmt"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
	"os"
	"strconv"
)

func main() {

	// 1: 创建一个生产者
	p, _ := rocketmq.NewProducer(
		producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"121.199.66.66:9876"})),
		producer.WithRetry(2),
	)

	err := p.Start()
	if err != nil {
		fmt.Println("启动生产者失败,%s", err.Error())
		os.Exit(1)
	}

	// 确定一个主题
	topic := "order_topic"
	for i := 0; i < 100; i++ {
		message := "Hello RocketMQ Go Client! " + strconv.Itoa(i)
		msg := &primitive.Message{
			Topic: topic,
			Body:  []byte(message),
		}
		// 这里选择严格的发送，发送成功就有回执
		res, err := p.SendSync(context.Background(), msg)
		if err != nil {
			fmt.Printf("send message error: %s\n", err)
		} else {
			// 消息发送回执
			fmt.Printf("send message success: result=%s : message=%s\n", res.String(), message)
		}

	}
	err = p.Shutdown()
	if err != nil {
		fmt.Printf("shutdown producer error: %s", err.Error())
	}
}
